package com.msxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msxt.dto.ShangjiaEntity;
import com.msxt.mapper.ShangjiaDao;
import com.msxt.result.Query;
import com.msxt.service.ShangjiaService;
import com.msxt.utils.PageUtils;
import com.msxt.view.ShangjiaView;
import com.msxt.vo.ShangjiaVO;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 商家服务实现类
 */
@Service("shangjiaService")
public class ShangjiaServiceImpl extends ServiceImpl<ShangjiaDao, ShangjiaEntity> implements ShangjiaService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		Page<ShangjiaEntity> page = this.page(
				new Query<ShangjiaEntity>(params).getPage(),
				new QueryWrapper<>()
		);
		return new PageUtils(page);
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params, QueryWrapper<ShangjiaEntity> wrapper) {
		Page<ShangjiaView> page = new Query<ShangjiaView>(params).getPage();

		// === 添加空条件检查 ===
		if (wrapper.isEmptyOfNormal()) {
			wrapper.isNotNull("id"); // 添加永真条件
		}

		// === 手动处理分页计数 ===
		long total = baseMapper.selectCount(wrapper); // 使用MyBatis-Plus的selectCount
		page.setTotal(total);

		if (total > 0) {
			page.setRecords(baseMapper.listView(page, wrapper));
		} else {
			page.setRecords(Collections.emptyList());
		}

		return new PageUtils(page);
	}

	@Override
	public List<ShangjiaVO> listVO(QueryWrapper<ShangjiaEntity> wrapper) {
		return baseMapper.listVO(wrapper);
	}

	@Override
	public ShangjiaVO selectVO(QueryWrapper<ShangjiaEntity> wrapper) {
		return baseMapper.selectVO(wrapper);
	}

	@Override
	public List<ShangjiaView> listView(QueryWrapper<ShangjiaEntity> wrapper) {
		return baseMapper.listView(wrapper);
	}

	@Override
	public ShangjiaView selectView(QueryWrapper<ShangjiaEntity> wrapper) {
		return baseMapper.selectView(wrapper);
	}
}